<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("../_inc/procFunction.php");

$errMsg = "";
if(sxREQ("reqtype") == "save"){	
	$errMsg .= puLinkIns();
}
if(sxREQ("reqtype") == "") {
	$sSQL = "SELECT * FROM pu_link WHERE process_unit='".sxREQ('pu')."' AND name='".sxREQ('lnk')."'";
	$objSDAO->query($sSQL,"selPuLk");
	$row = $objSDAO->fetch("selPuLk");
	sxRDef('name', '');
	sxRDef('group', '');
	sxRDef('sequence', '100');
	sxRDef('label', '');
	sxRDef('tooltip', '');
	sxRDef('note', '');
}

//load processes 
$sSQL = "SELECT * FROM process_unit ORDER BY process_unit";
$objSDAO->query($sSQL, "puList");
$aValPU = array('- back -'=>'-- previous --','- self -'=>'-- self --');
while($rowPU = $objSDAO->fetch("puList")){
	$aValPU[$rowPU['process_unit']] = $rowPU['process_unit']." - ".$rowPU['description'];
}


$aOpz = array("kMenu"=>'pro.pru', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
echo(bodyHTML($aOpz));
?>
<script language='JavaScript' type='text/javascript'>
function objFrmSubm(rt){
	document.objFrm.reqtype.value = rt;
	document.objFrm.submit();
}
</script>
<?php 
echo getPuHeader(sxREQ('pu'));

$aCmd = array("ins"=>array("pag"=>"javascript:objFrmSubm('save');",
										"title"=>_t("save"),
										"text"=>_t("save")
										),
					"back"=>array()
					);
echo(commandsHTML($aCmd));	

echo getHtmlForm('objFrm');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('reqtype','save');
echo getHtmlHidden('pu');

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='2'>Link</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Name*</td>" .
			"<td class=\"text01\">".getHtmlText('name',32,32)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Linked process-unit*</td>" .
			"<td class=\"text01\">");
echo getHtmlSelect('pu_linked',$aValPU);
echo("</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Group</td>" .
			"<td class=\"text01\">".getHtmlText('group',32,32)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Sequence</td>" .
			"<td class=\"text01\">".getHtmlText('sequence',4,8)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">label</td>" .
			"<td class=\"text01\">".getHtmlText('label',40,80)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Tool-tip</td>" .
			"<td class=\"text01\">".getHtmlTextArea('tooltip',100,3)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Note</td>" .
			"<td class=\"text01\">".getHtmlTextArea('note',100,3)."</td>" .
		"</tr>\n");
echo("</table>");
echo(commandsHTML($aCmd));
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function puLinkIns($redirOnEnd = true){
 	global $objSDAO;
	$quote = "`";
	$errMsg = "";
	$_REQUEST['name'] = sxRTrim('name');
	if(sxREQ("name") == "")
		$errMsg .= "name missing<br>";
	if(preg_match("/[^a-z0-9_]/",sxREQUEST("name")))
		$errMsg .= "name: a-z 0-9 _ only<br>";
	
	//existing check
	$sSQL = "SELECT count(*) as num FROM pu_link WHERE process_unit='".sxRAS("pu")
		."' AND name='".sxRAS("name")."'";
	$num = 0;
	$objSDAO->query($sSQL,"test");
	$row = $objSDAO->fetch("test");
	if($row["num"] != 0)
		$errMsg .= "existing name<br>";
	if($errMsg != ""){		
		return $errMsg;
	}
	if (sxRTrim('label')=='') $_REQUEST['label'] = sxREQ('name');
	$sSQL = "INSERT INTO " .
			"pu_link " .
			"(process_unit, " .
			"name, " .
			"pu_linked, " .
			"`group`, " .
			"sequence, " .
			"label, " .
			"tooltip, " .
			"note)" .
			"VALUES('".sxRAS("pu")."', " .
			"'".sxRAS("name")."', " .
			"'".sxRAS("pu_linked")."', " .
			"'".sxRAS("group")."', " .
			"".sxRInt("sequence").", " .
			"'".sxRAS("label")."', " .
			"'".sxRAS("tooltip")."', " .
			"'".sxRAS("note")."'" .
			")";	
	
	$objSDAO->query($sSQL,"ins");
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);	
	return 'abnormal end';
}
